package com.lsy.controller;

import com.lsy.dao.AccountMapper;
import com.lsy.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * 项目名：springboot01
 * 包名：com.lsy.controller
 * 类名：BlogController
 * 描述：
 * 作者：柳胜酉
 * 开始时间：2023/6/12
 * 完成时间：
 * 版本：1.0
 */
@Controller
public class BlogController {
    @Autowired
    AccountMapper accountMapper;

    @RequestMapping("/")
    public String index(){
        return "/index";
    }

    @RequestMapping("/user/login")
    public String login(){
        return "login/login";
    }

    @RequestMapping("/user/register")
    public String register(){
        return "register/register";
    }

    @RequestMapping("/main")
    public String main(Authentication authentication){
        User userDetails= (User) authentication.getPrincipal();
        System.out.println(userDetails.getUsername());
        System.out.println(userDetails.getPassword());
        System.out.println(userDetails.getAuthorities());
        return "main";
    }

    @RequestMapping("/detail/common/{id}")
    public String commonDetail(@PathVariable int id){
        return "detail/common/"+id;
    }

    @RequestMapping("/detail/admin/{id}")
    public String vipDetail(@PathVariable int id){
        return "detail/amdin/"+id;
    }

    @RequestMapping("/noauth")
    public String noauth(){
        return "noauth";
    }

    @RequestMapping("/user/doRegister")
    public ModelAndView doRegister(Account account) {
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(account);

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        account.setPassword(encoder.encode(account.getPassword()));
        account.setRole(1);
        accountMapper.insert(account);
        modelAndView.setViewName("login/login");
        return modelAndView;
    }
}
